local GoodsData = require("app.datacenter.GoodsData")
local TradeData = require("app.datacenter.TradeData")
--马车背包格子类
local TransPackGrid = class("TransPackGrid")

function TransPackGrid:ctor()
    self.gdata = nil
    self.status = TPACK_STATUS_EMPTY
    self.index = 0 --位置
    --self.capacity = 100 --容量
    self.isown = true --买入物品时做区分
end

function TransPackGrid:getStatus()
    if self.gdata==nil or self.gdata.count == 0 then
        self.status = TPACK_STATUS_EMPTY
    elseif self.gdata.count<self.gdata.overlying then
        self.status = self.isown and TPACK_STATUS_OCCUPY or TPACK_STATUS_OCCUPY_UNSTORED
    else
        self.status = self.isown and TPACK_STATUS_FULL or TPACK_STATUS_FULL_UNSTORED
    end
    return self.status
end

function TransPackGrid:clearGoodsData()
    self.gdata = nil
    self.isown = true
end

--临时 回收车(与购物车相反)
local RecycleItem = class("RecycleItem")
function RecycleItem:ctor(id,count,idx)
    self.id = id
    self.items = {} --商品来源及数量 {idx,count}
    self.totalcount = 0 --总量 
    self:addItem(id,count,idx)
end
--加入
function RecycleItem:addItem(id,count,idx)
    if self.id==id then
        self.id = id
        self.totalcount = self.totalcount + count
        local binlist = false
        for k,v in pairs(self.items) do
            if v.idx == idx then
                v.count=v.count+count
                binlist = true
                break
            end
        end
        if not binlist then
            table.insert(self.items,{idx=idx,count=count})
        end
        return true
    else
        return false
    end
end
--取出
function RecycleItem:takeout(count)
    local out = {}
    self.totalcount = self.totalcount - count
    for i=#self.items,1,-1 do
        local item = self.items[i]
        if count<= item.count then
            item.count = item.count - count
            table.insert(out,{idx=item.idx,count=count})       
            return out
        else
            count = count - item.count
            table.insert(out,item)      
            table.remove(self.items,i)
        end
    end
    return out
end

function RecycleItem:getTotalCount()
    return self.totalcount
end

--运输设备背包管理
local DyTransportPack = {
    packSize = 0,
    packs = {},
    recycleItems = {},
}

function DyTransportPack:setPacks(maxcount)
    for i=self.packSize+1,maxcount do
        local tpg = TransPackGrid.new()
        tpg.isown = true
        tpg.index = i
        self.packs[i] = tpg
    end
    self.packSize = maxcount
end
--加入货物 （按照idx,不会堆叠)
function DyTransportPack:addItem(item)
    local tpg = self.packs[item.idx]
    if tpg.gdata == nil then
        tpg.gdata = GoodsData.new(item.id,item.count)
    elseif tpg.gdata.id == item.id then
        tpg.gdata:setCount(item.count+tpg.gdata.count)
    else
        printError("DyTransportPack:addItem error:",json.encode(item))
    end
end

--设置格子的货物
function DyTransportPack:setItem(item,isown)
    local tpg = self.packs[item.idx]
    tpg.isown = isown and true or false
    tpg.gdata = GoodsData.new(item.id,item.count)
end

function DyTransportPack:getItem(index)
    return self.packs[index]
end

function DyTransportPack:clearItem(index)
    local tpg = self.packs[index]
    if tpg then
        tpg:clearGoodsData()
    end
end

function DyTransportPack:getGoodsData(index)
    return self.packs[index].gdata
end
--
function DyTransportPack:getPacks()
    return self.packs
end

function DyTransportPack:setDragState(state)
    self.isDragingItems = state
end
function DyTransportPack:getDragState()
    return self.isDragingItems
end

--背包 购物车内的
function DyTransportPack:getCartItems()
    local t = {}
    for k,v in pairs(self.packs) do
        if v:getStatus() ==TPACK_STATUS_OCCUPY_UNSTORED or v:getStatus() ==TPACK_STATUS_FULL_UNSTORED then
            table.insert(t,v)
        end
    end
    return t
end
--清理 购物车内的
function DyTransportPack:clearCartItems()
    for k,v in pairs(self.packs) do
        if v:getStatus() ==TPACK_STATUS_OCCUPY_UNSTORED or v:getStatus() ==TPACK_STATUS_FULL_UNSTORED then
            self:undoPurchase(k,v.gdata:getCount())
        end
    end
end
-- 暂时只用服务器的数据
function DyTransportPack:calCartItems()
    for k,v in pairs(self.packs) do
        if v:getStatus() ==TPACK_STATUS_OCCUPY_UNSTORED or v:getStatus() ==TPACK_STATUS_FULL_UNSTORED then
            v:clearGoodsData()
        end
    end
end
--回收车
function DyTransportPack:getRecycleItems()
    return self.recycleItems
end
--清空回收车
function DyTransportPack:clearRecycleItems(undosell)
    undosell = undosell==nil and true or false
    if undosell then
        for k,v in pairs(self.recycleItems) do
            self:undoSell(v.id,v.totalcount)
        end
    end
    self.recycleItems = {}
end

--购买商品
function DyTransportPack:purchaseGoods(itemid,itemcount)
    --数量
    local count = itemcount
    --放入背包
    for k,tpg in pairs(self.packs) do
        --空的格子或者是 非原有货物且未放满
        if tpg:getStatus()==TPACK_STATUS_EMPTY or (tpg:getStatus()==TPACK_STATUS_OCCUPY_UNSTORED and tpg.gdata.id==itemid) then
            if tpg.gdata == nil then
                tpg.gdata = GoodsData.new(itemid,0)
            end
            local left = tpg.gdata:getCapacity() - tpg.gdata.count
            count = count - left
            --已全部放入
            if count<=0 then
                tpg.gdata:setCount(tpg.gdata:getCapacity()+count)
                tpg.isown = false
                count = 0
                break
            else
                tpg.gdata:setCount(tpg.gdata:getCapacity())
                tpg.isown = false         
            end           
        end
    end
    --商店实际购买
    DyTradeStore:purchaseGoods(itemid,itemcount-count)
    if count>0 then
        return false
    else
        return true
    end
end
--撤销购买
function DyTransportPack:undoPurchase(index,count)
    local tpg = self.packs[index] 
    DyTradeStore:undoPurchase(tpg.gdata.id,count)

    local left = tpg.gdata:getCount() - count
    if left == 0 then
        tpg:clearGoodsData()
    else
        tpg.gdata:setCount(left)
    end
    
    DyTradeStore:undoPurchase(itemid,count)

    return true
end
--卖出马车商品
function DyTransportPack:sellGoods(packindex,itemcount)
    local id = self.packs[packindex].gdata.id
    local bInRecycleShop = false
    for i=1,#self.recycleItems do
        local rsd = self.recycleItems[i]
        if rsd.id == id then
            rsd:addItem(id,itemcount,packindex)
            bInRecycleShop = true
            break
        end      
    end
    --回收车里没有该商品  则插入该商品
    if not bInRecycleShop then
        local rsd = RecycleItem.new(id,itemcount,packindex)
        table.insert(self.recycleItems,rsd)
    end
    --刷新马车背包
    self.packs[packindex].gdata.count = self.packs[packindex].gdata.count - itemcount
    if self.packs[packindex].gdata:getCount()==0 then
        self.packs[packindex]:clearGoodsData()
    end
    return true
end
--撤销卖出
function DyTransportPack:undoSell(id,count)
    local out = {}
    --回收车里取出
    for k,item in pairs(self.recycleItems) do
        if item.id == id then
             out = item:takeout(count)
             if item.totalcount == 0 then
                table.remove(self.recycleItems,k)
             end
             break
        end
    end
    --刷新马车背包
    for k,item in pairs(out) do
        self:addItem({idx=item.idx,count=item.count,id=id})
    end
    return {}
end
--执行物品拖拽
function DyTransportPack:doPackDrag(fromIndex,toIndex)
    local tpgfrom= self.packs[fromIndex]
    local tpgto = self.packs[toIndex]
    if tpgto == nil then
        printError("%s","position not vaild")
        return
    end
    --原有物品不能和购入物品进行操作
    if tpgto.isown~= tpgto.isown and tpgto:getStatus()~=TPACK_STATUS_EMPTY then
        return
    end
    
    if tpgto:getStatus() == TPACK_STATUS_EMPTY then
        --放入空栏
        tpgto.gdata = tpgfrom.gdata
        tpgto.isown = tpgfrom.isown
        tpgfrom:clearGoodsData()
    elseif tpgfrom.gdata and tpgto.gdata and tpgfrom.gdata.id == tpgto.gdata.id then
        --合并相同物品
        local left = tpgto.gdata.overlying - tpgto.gdata.count
        if left >= tpgfrom.gdata.count then
            --原物品消失
            tpgto.gdata.count = tpgto.gdata.count + tpgfrom.gdata.count
            tpgfrom:clearGoodsData()
        elseif left == 0 then
            --交换位置
            self.packs[fromIndex] = tpgto
            self.packs[fromIndex].index = fromIndex
            self.packs[toIndex] = tpgfrom
            self.packs[toIndex].index = toIndex
        else
            --合并
            tpgfrom.gdata.count = tpgfrom.gdata.count - left
            tpgto.gdata.count = tpgto.gdata.overlying
        end   
    else
        --交换物品
        self.packs[fromIndex] = tpgto
        self.packs[fromIndex].index = fromIndex
        self.packs[toIndex] = tpgfrom
        self.packs[toIndex].index = toIndex
    end
end

function DyTransportPack:reset()
    self.packSize = 0
    self.packs = {}
end

return DyTransportPack